package com.xiaojiang.service.impl;

import com.xiaojiang.mapper.UserMapper;
import com.xiaojiang.pojo.User;
import com.xiaojiang.service.UserService;
import com.xiaojiang.utils.Md5Util;
import com.xiaojiang.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 功能：
 * 作者：小江
 * 日期：2024年08月01日
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        User u = userMapper.findByUsername(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        //密码加密
        String md5String = Md5Util.getMD5String(password);

        //添加
        userMapper.add(username,md5String);
    }

    @Override
    public void updateInfo(User user) {
        //将UpdateTime赋值
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateInfo(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");

        String md5String = Md5Util.getMD5String(newPwd);
        userMapper.updatePwd(md5String,id);
    }
}
